<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">

        <!-- 用了一个行列布局 -->
        <tree-tool :tree-node="company" :is-root="true" @addDeparts="addDeparts" />

        <!-- 组织结构 -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <tree-tool slot-scope="{data}" :tree-node="data" @delDepts="getDepartments" @emitDepts="emitDepts" @addDeparts="addDeparts" />
        </el-tree>
        <add-tool ref="addtool" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
      </el-card>
    </div>
  </div>
</template>

<script>
import TreeTool from './components/TreeTool.vue'
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils/index'
import AddTool from './components/add-tool.vue'
export default {
  components: { TreeTool, AddTool },
  data() {
    return {
      defaultProps: {
        label: 'name'
      },
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '' },
      departs: [], // 部列表
      showDialog: false, // 是否弹框
      node: null, // 当前节点对象
      loading: false
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async  getDepartments() {
      this.loading = true
      const res = await getDepartments()
      // console.log(res)
      this.departs = transListToTreeData(res.depts, '')
      this.company = { name: res.companyName, manager: res.companyManage || '负责人', id: '' }
      this.loading = false
    },
    // 新增
    addDeparts(node) {
      this.showDialog = true
      this.node = node // 当前点击项数据
    },
    // 编辑
    emitDepts(node) {
      this.showDialog = true
      this.node = node
      console.log(this.$refs.addtool, 'addtool')
      // 获取add-tool组件的实例  调用获取部门详细
      this.$refs.addtool.getDepartDetail(this.node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
